/* This program runs initial regs on the comparable-to-original data.

   Feb 12, 2020
   
*/






log using "reg_maindata_mylog.log", replace

global path "[redacted]"
global programs "${path}programs/"
global logs "${path}logs/"
global rawlbd "/projects/data/lbd/"
global imported "[redacted]"
global inter "${path}data/inter/"
global output "${path}data/output/"
global tables "${path}results/tables/"
global graphs "${path}results/graphs/"



 use ${output}lbd_innovindex.dta, clear

foreach var in ewppw ewavgestsize ewentrate {
   gen l2`var'=ln(`var'+(`var'^2+1)^0.5)
}

replace pay = pay/1000

foreach year of numlist 1977/1979, 1981/1997 {
      gen xzcuse1980I`year' = 0
      replace xzcuse1980I`year' = `var' if year==`year'
}

preserve
   keep year numfips numsic emp l2ppw *zcuse1980*
   save ${output}lbd_forgraphs.dta, replace
restore


*****************
*** Tables   ***
*****************

reghdfe emp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
gen samp1=1 if e(sample)

** Table 1 **
gen univcntyemp = emp if univcont==1
gen univsurcntyemp = emp if univsur==1
gen univcntyppw = ppw if univcont==1
gen univsurcntyppw = ppw if univsur==1

* Panel A
tabstat emp univcntyemp univsurcntyemp avgestsize pay ppw univcntyppw univsurcntyppw numestabs zcuse1980 if samp1==1, stats(n mean sd min max) c(stat)
* have to add fed funding

* Panel B
gen emp97 = emp if year==1997
gen emp77 = emp if year==1977
* create county-industry unique id
egen fipssic = concat(fips sic3)
destring fipssic, replace
preserve
   keep if year==1997 | year==1977
   gen period = 1 if year==1977
   replace period = 2 if year==1997
   xtset fipssic period
   gen demp = d.emp
   gen gremp1 = d.emp/l.emp
   gen baseemp = l.emp
   gen avgbaseemp = 0.5*(emp+l.emp)
   gen gremp2 = d.emp/(0.5*(emp+l.emp))
   * two versions, one for just containing counties, one for containing and surrounding
   tabstat demp baseemp avgbaseemp gremp1 gremp2 if samp1==1 & univcont==1, stats(n mean sd min max) c(stat)
   tabstat demp baseemp avgbaseemp gremp1 gremp2 if samp1==1 & univsur==1, stats(n mean sd min max) c(stat)
   tabstat demp baseemp avgbaseemp gremp1 gremp2 if samp1==1 , stats(n mean sd min max) c(stat)
restore

* Panel C: county level variables
preserve
   keep if samp==1
   * collapse to county-year level, then to county
   collapse (count) numinds=numsic (sum) cntyemp=emp , by(fips numfips year univcont univsur)
   gen cntyemp77 = cntyemp if year==1977
   gen cntyemp97 = cntyemp if year==1997
   
   * collapse to county level
   collapse (mean) numinds cntyemp (max) cntyemp77 cntyemp97, by(fips numfips univcont univsur)
   gen demp = cntyemp97-cntyemp77
   gen avgbaseemp = 0.5*(cntyemp77+cntyemp97)
   gen gremp1 = demp/cntyemp77
   gen gremp2 = demp/avgbaseemp
   tabstat numinds cntyemp77 cntyemp97 avgbaseemp demp gremp1 gremp2 if univcont==1, stat(n mean sd min max) c(stat)
   tabstat numinds cntyemp77 cntyemp97 avgbaseemp demp gremp1 gremp2 if univsur==1, stats(n mean sd min max) c(stat)
   tabstat numinds cntyemp77 cntyemp97 avgbaseemp demp gremp1 gremp2, stats(n mean sd min max) c(stat)
restore

*** END TABLE 1 ***
*******************

***************
*** TABLE 2 ***

** Table 2 **

* emp
reghdfe emp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
* avg est size
reghdfe avgestsize xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
* num estabs
reghdfe numestabs xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
* pay
reghdfe pay xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
* wage (emp weighted)
reghdfe ewppw xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
* log wage
reghdfe l1ppw xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  


* for scaling the effect (text):
reghdfe emp xabdcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  

*** END TABLE 2 ***
*******************

*******************************
*** TABLE 3, Panel A, Row 2 ***

* emp
reghdfe emp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
* avg est size
reghdfe avgestsize xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
* num estabs
reghdfe numestabs xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  


*** END TABLE 3, Panel A, Row 2 ***
***********************************

************************
*** TABLE 3, Panel B ***
************************

* Initial county emp (cntyemp1977)
* Initial pop density (popdens)
* Initial BA share (bashare)

gen cntyemp1977thou = cntyemp1977/1000
gen popdensthou = popdens/1000
gen misspopdens = (popdens==.)
egen meanpopdensthou = mean(popdensthou)
replace popdensthou = meanpopdensthou if popdensthou==.
gen missbashare = (bashare==.)
egen meanbashare = mean(bashare)
replace bashare = meanbashare if bashare==.
label var cntyemp1977thou "1977 County Emp (Thou)"
label var popdensthou "1980 Pop Density (Thou/sqmi)"
* calc interactions needed: size-abd-index, size-abd, size-index
foreach var in cntyemp1977thou popdensthou bashare {
   gen xabd`var' = abd*`var'
   gen x`var'zcuse1980 = `var'*zcuse1980
   gen xx`var'abdzcuse1980 = `var'*abd*zcuse1980
}


reghdfe emp xxcntyemp1977thouabdzcuse1980 xabdzcuse1980, absorb(numsic#year numfips#year numfips#numsic) vce(cluster numfips)  

reghdfe emp xxpopdensthouabdzcuse1980 xabdzcuse1980, absorb(numsic#year numfips#year numfips#numsic) vce(cluster numfips)  

reghdfe emp xxbashareabdzcuse1980 xabdzcuse1980, absorb(numsic#year numfips#year numfips#numsic) vce(cluster numfips)  


reghdfe avgestsize xxcntyemp1977thouabdzcuse1980 xabdzcuse1980, absorb(numsic#year numfips#year numfips#numsic) vce(cluster numfips)  

reghdfe avgestsize xxpopdensthouabdzcuse1980 xabdzcuse1980, absorb(numsic#year numfips#year numfips#numsic) vce(cluster numfips)  

reghdfe avgestsize xxbashareabdzcuse1980 xabdzcuse1980, absorb(numsic#year numfips#year numfips#numsic) vce(cluster numfips)  


reghdfe l2ewppw xxcntyemp1977thouabdzcuse1980 xabdzcuse1980, absorb(numsic#year numfips#year numfips#numsic) vce(cluster numfips)  

reghdfe l2ewppw xxpopdensthouabdzcuse1980 xabdzcuse1980, absorb(numsic#year numfips#year numfips#numsic) vce(cluster numfips)  

reghdfe l2ewppw xxbashareabdzcuse1980 xabdzcuse1980, absorb(numsic#year numfips#year numfips#numsic) vce(cluster numfips)  


****************************
*** END TABLE 3, Panel B ***
****************************


************************
*** TABLE 4 ***
************************


merge m:1 fips using ${inter}fedfunding_surcnty.dta

* values will now be in millions instead of thousands
foreach var in fvyrsumTOT fvyrsumDOD fvyrsumNIH avgannTOT avgannDOD avgannNIH {
   replace `var' = 0 if _merge==1
   replace `var' = `var'/1000

}
drop _merge

foreach var in avgannTOT avgannNIH {
   foreach year of numlist 1977/1979, 1981/1997 {
      gen x`var'I`year' = 0
      replace x`var'I`year' = `var' if year==`year'
      gen xx`var'zcuse1980I`year' = 0
      replace xx`var'zcuse1980I`year' = `var'*zcuse1980 if year==`year'
   }
}


preserve 
   keep year numfips numsic emp l2ppw *avgann* *zcuse1980*
   save ${output}lbd_fedfunding_forgraphs.dta, replace
restore

tabstat fvyrsumTOT fvyrsumDOD fvyrsumNIH avgannTOT avgannDOD avgannNIH if samp1==1, stats(n mean sd min max) c(stat)

foreach var in fvyrsumTOT fvyrsumDOD fvyrsumNIH avgannTOT avgannDOD avgannNIH {
   gen xabd`var' = abd*`var'
   gen xxabd`var'zcuse1980 = abd*`var'*zcuse1980
}

reghdfe emp xabdavgannTOT, absorb(numsic#year numsic#numfips) vce(cluster numfips)
reghdfe emp xabdavgannDOD, absorb(numsic#year numsic#numfips) vce(cluster numfips)
reghdfe emp xabdavgannNIH, absorb(numsic#year numsic#numfips) vce(cluster numfips)


reghdfe emp xxabdavgannTOTzcuse1980 xabdzcuse1980 xabdavgannTOT, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfip)
reghdfe emp xxabdavgannDODzcuse1980 xabdzcuse1980 xabdavgannDOD, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)
reghdfe emp xxabdavgannNIHzcuse1980 xabdzcuse1980 xabdavgannNIH, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfip)


************************
*** END TABLE 4 ***
************************

***************
*** TABLE 5 *** 
***************

reghdfe emp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  

reghdfe es1emp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
reghdfe es2emp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
reghdfe es3emp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
reghdfe es4emp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  

reghdfe incemp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  

reghdfe es1incemp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
reghdfe es2incemp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
reghdfe es3incemp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
reghdfe es4incemp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  

reghdfe entemp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  

reghdfe es1entemp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
reghdfe es2entemp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
reghdfe es3entemp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
reghdfe es4entemp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  

reghdfe suentemp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  

reghdfe es1suentemp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
reghdfe es2suentemp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
reghdfe es3suentemp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
reghdfe es4suentemp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  

reghdfe muentemp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  

reghdfe es1muentemp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
reghdfe es2muentemp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
reghdfe es3muentemp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
reghdfe es4muentemp xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  


* use in text; creative destruction
reghdfe survincemp97 xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  
reghdfe dyincemp97 xabdzcuse1980, absorb(numsic#year numfips#year numsic#numfips) vce(cluster numfips)  


*******************
*** END TABLE 5 *** 
*******************


************************
*** FIGURE 1 ***
************************

local infile ${output}lbd_innovindex_forgraphs.dta
local depvar emp l2ppw

foreach dep in `depvar' {
      use `infile', clear

      if "`dep'"=="emp" {
      reghdfe `dep' xzncuse1980I1977-xzncuse1980I1979 xzncuse1980I1981-xzncuse1980I1997, absorb(numsic3#year numfips#year numfips#numsic) vce(cluster numfips)

      gen graphbeta=.
      gen graphlci=.
      gen graphuci=.
      gen runtime=.

      foreach i of numlist 1977/1979, 1981/1997 {
	  replace graphbeta=_b[xzcuse1980I`i'] in `=`i'-1976'
	  replace graphlci=(_b[xzcuse1980I`i'] -1.96*abs(_se[xzcuse1980I`i'])) in `=`i'-1976'
	  replace graphuci=(_b[xzcuse1980I`i'] +1.96*abs(_se[xzcuse1980I`i'])) in `=`i'-1976'
	  replace runtime=`=`i'' in `=`i'-1976'
      }
      replace graphbeta=0 in 4
      replace graphlci=0 in 4
      replace graphuci=0 in 4
      replace runtime=1980 in 4

	  twoway (connected graphbeta runtime if runtime<1980, mcolor(black) lcolor(black)) (connected graphbeta runtime if runtime>1978 & runtime<1981, mcolor(black) lcolor(black)) (connected graphbeta runtime if runtime>=1980, mcolor(black) lcolor(black)) (rcap graphuci graphlci runtime, msymbol(dot) mcolor(gray) lcolor(navy) lp(dash)), xline(1980.5, lp(dash)) yline(0, lc(gs2)) ytick(, grid) xlabel(1977(2)1997) plotregion(margin(0)) legend(off) graphregion(color(white)) bgcolor(white) xtit("Year") ytit(Employment (LBD))
      }

      else if "`dep'"=="l2ppw"  {
	        reghdfe `dep' xzcuse1980I1977-xzcuse1980I1979 xzcuse1980I1981-xzcuse1980I1997, absorb(numsic3#year numfips#year numfips#numsic) vce(cluster numfips)

      gen graphbeta=.
      gen graphlci=.
      gen graphuci=.
      gen runtime=.

      foreach i of numlist 1977/1979, 1981/1997 {
	  replace graphbeta=_b[xzcuse1980I`i'] in `=`i'-1976'
	  replace graphlci=(_b[xzcuse1980I`i'] -1.96*abs(_se[xzcuse1980I`i'])) in `=`i'-1976'
	  replace graphuci=(_b[xzcuse1980I`i'] +1.96*abs(_se[xzcuse1980I`i'])) in `=`i'-1976'
	  replace runtime=`=`i'' in `=`i'-1976'
      }
      replace graphbeta=0 in 4
      replace graphlci=0 in 4
      replace graphuci=0 in 4
      replace runtime=1980 in 4

	  twoway (connected graphbeta runtime if runtime<1980, mcolor(black) lcolor(black)) (connected graphbeta runtime if runtime>1978 & runtime<1981, mcolor(black) lcolor(black)) (connected graphbeta runtime if runtime>=1980, mcolor(black) lcolor(black)) (rcap graphuci graphlci runtime, msymbol(dot) mcolor(gray) lcolor(navy) lp(dash)), xline(1980.5, lp(dash)) yline(0, lc(gs2)) ytick(, grid) xlabel(1977(2)1997) plotregion(margin(0)) legend(off) graphregion(color(white)) bgcolor(white) xtit("Year") ytit(Log Wages (LBD))
      }
      

      graph export ${graphs}`dep'_zcuse1980_cxyixycxiFE.eps, replace
      !epstopdf ${graphs}`dep'_zcuse1980_cxyixycxiFE.eps

      estadd scalar Nround=round(e(N),500)
      estimates store `dep'_`var'zcu80
 }
 


esttab emp_zcu80 l2ppw_zcu80 using ${tables}evstud_lvl_zcu80_cxyixycxi.txt, replace style(tab) cells(b(star fmt(4)) se(par fmt(4))) title(Event Study Coefficients)



************************
*** END FIGURE 1 ***
************************


************************
*** FIGURE 3 ***
************************


local infile ${output}lbd_fedfunding_forgraphs.dta
local depvar emp l2ppw
local indvar  avgannTOT avgannNIH 


foreach dep in `depvar' {
   foreach var in `indvar' {
      use `infile', clear


      reghdfe `dep' xx`var'zcuse1980I1977-xx`var'zcuse1980I1979 xx`var'zcuse1980I1981-xx`var'zcuse1980I1997  xabdzcuse1980, absorb(numsic3#year numfips#year numfips#numsic) vce(cluster numfips)

      gen graphbeta=.
      gen graphlci=.
      gen graphuci=.
      gen runtime=.

      foreach i of numlist 1977/1979, 1981/1997 {
	  replace graphbeta=_b[xx`var'zcuse1980I`i'] in `=`i'-1976'
	  replace graphlci=(_b[xx`var'zcuse1980I`i'] -1.96*abs(_se[xx`var'zcuse1980I`i'])) in `=`i'-1976'
	  replace graphuci=(_b[xx`var'zcuse1980I`i'] +1.96*abs(_se[xx`var'zcuse1980I`i'])) in `=`i'-1976'
	  replace runtime=`=`i'' in `=`i'-1976'
      }
      replace graphbeta=0 in 4
      replace graphlci=0 in 4
      replace graphuci=0 in 4
      replace runtime=1980 in 4

      if "`dep'"=="emp" {
	  twoway (connected graphbeta runtime if runtime<1980, mcolor(black) lcolor(black)) (connected graphbeta runtime if runtime>1978 & runtime<1981, mcolor(black) lcolor(black)) (connected graphbeta runtime if runtime>=1980, mcolor(black) lcolor(black)) (rcap graphuci graphlci runtime, msymbol(dot) mcolor(gray) lcolor(navy) lp(dash)), xline(1980.5, lp(dash)) yline(0, lc(gs2)) ytick(, grid) xlabel(1977(2)1997) plotregion(margin(0)) legend(off) graphregion(color(white)) bgcolor(white) xtit("Year") ytit(Employment (LBD))
      }

      else if "`dep'"=="l2ppw"  {
	  twoway (connected graphbeta runtime if runtime<1980, mcolor(black) lcolor(black)) (connected graphbeta runtime if runtime>1978 & runtime<1981, mcolor(black) lcolor(black)) (connected graphbeta runtime if runtime>=1980, mcolor(black) lcolor(black)) (rcap graphuci graphlci runtime, msymbol(dot) mcolor(gray) lcolor(navy) lp(dash)), xline(1980.5, lp(dash)) yline(0, lc(gs2)) ytick(, grid) xlabel(1977(2)1997) plotregion(margin(0)) legend(off) graphregion(color(white)) bgcolor(white) xtit("Year") ytit(Log Wages (LBD))
      }
      

      graph export ${graphs}ff_`dep'_`var'zcuse1980_cxyixycxiFE.eps, replace
      !epstopdf ${graphs}ff_`dep'_`var'zcuse1980_cxyixycxiFE.eps

      estadd scalar Nround=round(e(N),500)
      estimates store ff`dep'_`var'zcu80
  }
}

* scale
replace ffemp_`var'zcu80 = ffemp_`var'zcu80/1000
replace ffl2ppw_`var'zcu80 = ffl2ppw_`var'zcu80/1000
foreach var in avgannTOT avgannNIH {
   esttab ffemp_`var'zcu80 ffl2ppw_`var'zcu80 using ${tables}ffevstud_lvl_`var'zcu80_cxyixycxi.txt, replace style(tab) cells(b(star fmt(4)) se(par fmt(4))) title(Event Study Coefficients)
}


************************
*** END FIGURE 3 ***
************************



  

log close





